﻿/* ************************************************************
 * Namespace: JumpFleetGameLib.xGraphicModes
 * Class:  SupportedGraphicModes
 * Author:  Roy Penrod
 * 
 * SupportedGraphicModes chooses the highest supported 
 * graphics mode based on the graphics adapter.
 * 
 * The Xbox 360 works with three graphic modes:
 * 480p (normal) = 640 x 480
 * 720p (widescreen) = 1280 x 720
 * 1080p (widescreen) = 1920 x 1080
 *  
 * ************************************************************ */

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace JumpFleetGameLib.xGraphicModes
{
    public class SupportedGraphicsModes
    {
        #region MEMBERS

        // holds an enum identifying the current graphics mode
        private static GraphicsModeAspect _currentGraphicsMode = GraphicsModeAspect.noneSelected;

        // flags for whether or not certain graphics modes are supported
        private static bool _480pSupported = false;
        private static bool _720pSupported = false;
        private static bool _1080pSupported = false;

        // holds the screen dimensions
        private static int _screenWidth;
        private static int _screenHeight;

        #endregion


        #region PROPERTIES            
        
        /// <summary>
        /// Returns the current graphics mode.  Type: enum GraphicsModeAspect
        /// </summary>
        public static GraphicsModeAspect CurrentGraphicsMode
        {
            get { return _currentGraphicsMode; }
        }

        /// <summary>
        /// Returns the screen width.  Type: int
        /// </summary>
        public static int ScreenWidth
        {
            get { return _screenWidth; }
        }

        /// <summary>
        /// Returns the screen height.  Type: int
        /// </summary>
        public static int ScreenHeight
        {
            get { return _screenHeight; }
        }    

        #endregion


        #region CONSTRUCTORS

        #endregion


        #region METHODS

        /// <summary>
        /// Sets the graphics mode to the highest graphics mode supported by the graphics adapter.  
        /// </summary>
        /// <param name="graphics">The GraphicsDeviceManager object.</param>
        public static void SetGraphicsMode(GraphicsDeviceManager graphics, bool applyChanges)        
        {
            

            // checks to see which Xbox 360 graphics mode are supported
            foreach (DisplayMode displayMode in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes)
            {
                if ((displayMode.Width == 640) && (displayMode.Height == 480))
                {
                    _480pSupported = true;
                }
                else if ( (displayMode.Width == 1280) && (displayMode.Height == 720) )
                {
                    _720pSupported = true;
                }
                else if ( (displayMode.Width == 1920) && (displayMode.Height == 1080) )
                {
                    _1080pSupported = true;
                }
            }

            // sets the current graphics mode to the highest supported graphics mode
            if (_1080pSupported)
            {
                _currentGraphicsMode = GraphicsModeAspect.widescreen1080p;
                graphics.PreferredBackBufferWidth = 1920;
                graphics.PreferredBackBufferHeight = 1080;
                _screenWidth = 1920;
                _screenHeight = 1080;

                if (applyChanges)
                    graphics.ApplyChanges();

                return;
            }
            else if (_720pSupported)
            {
                _currentGraphicsMode = GraphicsModeAspect.widescreen720p;
                graphics.PreferredBackBufferWidth = 1280;
                graphics.PreferredBackBufferHeight = 720;
                _screenWidth = 1280;
                _screenHeight = 720;

                if (applyChanges)
                    graphics.ApplyChanges();

                return;
            }
            else if (_480pSupported)
            {
                _currentGraphicsMode = GraphicsModeAspect.normal480p;
                graphics.PreferredBackBufferWidth = 640;
                graphics.PreferredBackBufferHeight = 480;
                _screenWidth = 640;
                _screenHeight = 480;

                if (applyChanges)
                    graphics.ApplyChanges();

                return;
            }
            else
            {
                _currentGraphicsMode = GraphicsModeAspect.noCompatibleGraphicsModes;
                throw new Exception("There aren't any compatible graphics modes for this device.");
            }
        }

        #endregion

    } // <-- end SupportedGraphicModes class
} // <-- end xGraphicModes namespace


